home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / liboctave / chMatrix.cc < prev    next >
C/C++ Source or Header  |  1997-01-24  |  3KB  |  189 lines

  1. // Matrix manipulations.
  2. /*
  3.  
  4. Copyright (C) 1996 John W. Eaton
  5.  
  6. This file is part of Octave.
  7.  
  8. Octave is free software; you can redistribute it and/or modify it
  9. under the terms of the GNU General Public License as published by the
  10. Free Software Foundation; either version 2, or (at your option) any
  11. later version.
  12.  
  13. Octave is distributed in the hope that it will be useful, but WITHOUT
  14. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16. for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with Octave; see the file COPYING.  If not, write to the Free
  20. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  21.  
  22. */
  23.  
  24. #if defined (__GNUG__)
  25. #pragma implementation
  26. #endif
  27.  
  28. #ifdef HAVE_CONFIG_H
  29. #include <config.h>
  30. #endif
  31.  
  32. #include <cstdio>
  33. #include <cstring>
  34.  
  35. #include <string>
  36.  
  37. #include <iostream.h>
  38.  
  39. // #include <sys/types.h>  // XXX FIXME XXX
  40.  
  41. #include "lo-error.h"
  42. #include "str-vec.h"
  43. #include "mx-base.h"
  44. #include "mx-inlines.cc"
  45.  
  46. // charMatrix class.
  47.  
  48. charMatrix::charMatrix (const char *s)
  49.   : MArray2<char> ()
  50. {
  51.   int nc = s ? strlen (s) : 0;
  52.   int nr = s && nc > 0 ? 1 : 0;
  53.  
  54.   resize (nr, nc);
  55.  
  56.   for (int i = 0; i < nc; i++)
  57.     elem (0, i) = s[i];
  58. }
  59.  
  60. charMatrix::charMatrix (const string& s)
  61.   : MArray2<char> ()
  62. {
  63.   int nc = s.length ();
  64.   int nr = nc > 0 ? 1 : 0;
  65.  
  66.   resize (nr, nc);
  67.  
  68.   for (int i = 0; i < nc; i++)
  69.     elem (0, i) = s[i];
  70. }
  71.  
  72. charMatrix::charMatrix (const string_vector& s)
  73.   : MArray2<char> (s.length (), s.max_length (), 0)
  74. {
  75.   int nr = rows ();
  76.  
  77.   for (int i = 0; i < nr; i++)
  78.     {
  79.       int nc = s[i].length ();
  80.       for (int j = 0; j < nc; j++)
  81.     elem (i, j) = s[i][j];
  82.     }
  83. }
  84.  
  85. bool
  86. charMatrix::operator == (const charMatrix& a) const
  87. {
  88.   if (rows () != a.rows () || cols () != a.cols ())
  89.     return 0;
  90.  
  91.   return equal (data (), a.data (), length ());
  92. }
  93.  
  94. bool
  95. charMatrix::operator != (const charMatrix& a) const
  96. {
  97.   return !(*this == a);
  98. }
  99.  
  100. charMatrix&
  101. charMatrix::insert (const char *s, int r, int c)
  102. {
  103.   if (s)
  104.     {
  105.       int s_len = strlen (s);
  106.  
  107.       if (r < 0 || r >= rows () || c < 0 || c + s_len - 1 > cols ())
  108.     {
  109.       (*current_liboctave_error_handler) ("range error for insert");
  110.       return *this;
  111.     }
  112.  
  113.       for (int i = 0; i < s_len; i++)
  114.     elem (r, c+i) = s[i];
  115.     }
  116.   return *this;
  117. }
  118.  
  119. charMatrix&
  120. charMatrix::insert (const charMatrix& a, int r, int c)
  121. {
  122.   Array2<char>::insert (a, r, c);
  123.   return *this;
  124. }
  125.  
  126. string
  127. charMatrix::row_as_string (int r, bool strip_ws = false) const 
  128. {
  129.   string retval;
  130.  
  131.   int nr = rows ();
  132.   int nc = cols ();
  133.  
  134.   if (r == 0 && nr == 0 && nc == 0)
  135.     return retval;
  136.  
  137.   if (r < 0 || r >= nr)
  138.     {
  139.       (*current_liboctave_error_handler) ("range error for row_as_string");
  140.       return retval;
  141.     }
  142.  
  143.   retval.resize (nc, '\0');
  144.  
  145.   for (int i = 0; i < nc; i++)
  146.     retval[i] = elem (r, i);
  147.  
  148.   if (strip_ws)
  149.     {
  150.       while (--nc >= 0)
  151.     {
  152.       char c = retval[nc];
  153.       if (c && c != ' ')
  154.         break;
  155.     }
  156.     }
  157.   else
  158.     {
  159.       while (--nc >= 0)
  160.     if (retval[nc])
  161.       break;
  162.     }
  163.  
  164.   retval.resize (nc+1);
  165.  
  166.   return retval;
  167. }
  168.  
  169. charMatrix
  170. charMatrix::transpose (void) const
  171. {
  172.   int nr = rows ();
  173.   int nc = cols ();
  174.   charMatrix result (nc, nr);
  175.   if (length () > 0)
  176.     {
  177.       for (int j = 0; j < nc; j++)
  178.     for (int i = 0; i < nr; i++)
  179.       result.elem (j, i) = elem (i, j);
  180.     }
  181.   return result;
  182. }
  183.  
  184. /*
  185. ;;; Local Variables: ***
  186. ;;; mode: C++ ***
  187. ;;; End: ***
  188. */
  189.